perf: valTag dispatch for O(1) materializer type routing#682
Open
He-Pin wants to merge 1 commit intodatabricks:masterfrom
Open
perf: valTag dispatch for O(1) materializer type routing#682He-Pin wants to merge 1 commit intodatabricks:masterfrom
He-Pin wants to merge 1 commit intodatabricks:masterfrom
Conversation
Add a valTag: Byte abstract method to Val with TAG constants (0-7) for each concrete subclass, enabling JVM tableswitch O(1) dispatch in the materializer instead of linear pattern matching. Changes: - Val.scala: Add valTag abstract method and TAG_STR/NUM/TRUE/FALSE/NULL/ ARR/OBJ/FUNC constants (0-7 contiguous range) - Materializer.scala: Replace pattern-match in materializeRecursiveChild with @switch tableswitch dispatch on valTag. Hoist xs.length out of materializeRecursiveArr while-loop. - CustomValTests.scala: Add valTag=-1 to ImportantString (custom Val) JMH improvements: reverse -2.9%, base64DecodeBytes -4.6%, comparison2 -1.9%, base64 -2.1%. No regressions outside noise range. Upstream: he-pin/sjsonnet jit branch commits 30b7495, 9ddb1a5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
The materializer uses
isInstanceOf/ pattern matching to determine the runtime type of eachValbefore converting it toujson.Value. This creates a chain of type checks for every value materialized. For large JSON outputs (likerealistic2with thousands of values), this overhead accumulates.Key Design Decision
Add a
valTag: Bytefield toValthat encodes the runtime type as a numeric constant, enabling O(1) dispatch via a lookup table or switch instead of sequentialisInstanceOfchecks. EachValsubtype sets its tag at construction time.Modification
Val.scala: AddedvalTagfield toValbase class, with constants for each subtype (TAG_STR,TAG_NUM,TAG_OBJ, etc.)Materializer.scala: Changed type dispatch from pattern matching to tag-based switchvaltag_dispatch.jsonnetcovering all value types through materializationBenchmark Results
JMH (JVM, 3 iterations)
Hyperfine (Scala Native, 10 runs, vs master)
Analysis
References
he-pin/sjsonnetjit branch commit30b7495bResult
Consistent JVM improvement for materialization-heavy workloads. Neutral on Scala Native.